﻿using DAL;
using ModelLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using User = ModelLayer.User;

namespace BusinessLayer
{
    public class ShoppingListB : IFShoppingListB
    {

        public ShoppingListB()
        {
        }

        public void CreateShoppingList(int userId, string shoppingListName, long completionDate)
        {
            TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970,1,1,0,0,0);

            ShoppingListDB shoppingListDB = new ShoppingListDB();
            int shoppingListId = shoppingListDB.CreateShoppingList(userId, shoppingListName, (int)timeSpan.TotalSeconds, (int)completionDate);
            shoppingListDB.AddUserToShoppingList(shoppingListId, userId);
        }

        public void RemoveShoppingList(int shoppingListId)
        {
            ShoppingListDB shoppingListDB = new ShoppingListDB();
            shoppingListDB.RemoveShoppingList(shoppingListId);
        }

        public bool AddUserToShoppingList(int shoppingListId, string friendUserName, string uname,string tokenId)
        {
            var u = new UserB();
            var user = u.SearchUName(friendUserName);
            int fid = user.UserId;
            ShoppingListDB shoppingListDB = new ShoppingListDB();
            var s = new Security();
            if (s.Authenticate(uname, tokenId))
            {
                shoppingListDB.AddUserToShoppingList(shoppingListId, fid);
                return true;
            }
            return false; 
        }

        public bool DeleteUserFromShoppingList(int shoppingListId, string friendUserName, string uname, string tokenId)
        {
            var u = new UserB();
            var user = u.SearchUName(friendUserName);
            int fid = user.UserId;
            ShoppingListDB shoppingListDB = new ShoppingListDB();
            var s = new Security();
            if (s.Authenticate(uname, tokenId))
            {
                shoppingListDB.DeleteUserFromShoppingList(shoppingListId, fid);
                return true;
            }
            return false; 
        }

        public void AddProductToShoppingList(int shoppingListId, int productId, int amount)
        {
            ShoppingListDB shoppingListDB = new ShoppingListDB();
            shoppingListDB.AddProductToShoppingList(shoppingListId, productId, amount);
        }


        public void DeleteProductFromShoppingList(int shoppingListId, int productId)
        {
            var db = new ShoppingListDB();
            db.DeleteProductFromShoppingList(shoppingListId, productId);
        }

        public void MarkAsBought(int shoppingListId, int productId)
        {
            ShoppingListDB shoppingListDB = new ShoppingListDB();
            shoppingListDB.MarkAsBought(shoppingListId, productId);
        }


        public List<ModelLayer.ShoppingList> GetShoppingListsWithUname(string uname)
        {
            var db = new ShoppingListDB();
            return db.GetShoppingLists(uname);
        }


        public List<ModelLayer.Product> GetProductsWithListId(int listId)
        {
            var db = new ShoppingListDB();
            return db.GetProductsFromShoppingList(listId);
        }

        public List<User> GetSharedUsersForShoppingList(string uname, int slist)
        {
            var db = new ShoppingListDB();
            return db.GetSharedUsersForShoppingList(uname,slist);
        }
    }

}
